﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfPractice.ViewModel;

namespace WpfPractice.View
{
    /// <summary>
    /// BindingTest.xaml 的交互逻辑
    /// </summary>
    public partial class BindingTest : Window
    {
        public BindingTest()
        {
            InitializeComponent();
            this.DataContext = new BindingTestViewModel()
            {
                Detail = "测试",
                Info = "信息交流"
            };
            this.Loaded += BindingTest_Loaded;
        }

        private void BindingTest_Loaded(object sender, RoutedEventArgs e)
        {
            Point mousePosition = GetMousePosition();
            // 使用转换后的坐标来定位窗口
            this.Left = mousePosition.X - (this.ActualWidth / 2);
            this.Top = mousePosition.Y - (this.ActualHeight / 2);
        }
        private Point GetMousePosition()
        {
            // 获取当前屏幕鼠标位置
            System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
            Point wpfPoint = new Point(point.X, point.Y);

            // 转换屏幕坐标到WPF设备独立坐标
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            System.Windows.Media.Matrix matrix = hwndSource.CompositionTarget.TransformFromDevice;
            return matrix.Transform(wpfPoint);
        }
    }
}
